<?php

class Groups extends CI_Model {

	function __construct() {
    	parent::__construct();
    }

    public function get_groups_and_students($class_id=false) {
		// SELECT * FROM groups
		// LEFT JOIN group_members ON group_members.group_id = groups.group_id
		// LEFT JOIN users ON users.user_id = group_members.user_id
		$this->db->select('groups.group_name, users.first_name, users.last_name');
		$this->db->from('groups');
		$this->db->join('group_members', 'group_members.group_id = groups.group_id');
		$this->db->join('users', 'users.user_id = group_members.user_id');
		$this->db->join('classes', 'users.class_id = classes.class_id', 'left');

		// Restrict by class
		if ( $class_id && $class_id != 'all' ) { 
			$this->db->where('classes.class_id', $class_id); 
		}

		$this->db->order_by("groups.group_name"); 
		
		$query = $this->db->get();
		$result = $query->result_array();
		return $result;
    }
	
	public function get_group_list() {
		$this->db->select('group_id, group_name');
		$this->db->from('groups');
		$query = $this->db->get();
		$result = $query->result_array();
		return $result;
	}
	
	public function get_student_groups($user_id=NULL) {
		$this->db->select('groups.group_id, groups.group_name');
		$this->db->from('group_members');
		$this->db->join('groups', 'group_members.group_id = groups.group_id');
		$this->db->where('group_members.user_id', $user_id);
		
		$query = $this->db->get();
		$result = $query->result_array();
		return $result;
	}
	
	//Add a student to a group
	public function add_student_to_group($user_id,$group_id) {
		$data = array(
			'user_id'=>$user_id,
			'group_id'=>$group_id
		);
		
		$this->db->insert('group_members',$data);
	}
	
	public function student_in_group($user_id) {
		$this->db->select('*');
		$this->db->from('group_members');
		$this->db->where('user_id', $user_id);
		
		$query = $this->db->get();
		
		if ($query->num_rows() > 0) {
			return true;
		} else {
			return false;
		}
	}

	public function group_members($group_id, $full_data = false) {
		if ($full_data) {
			$this->db->select("*"); 
		} else {
			$this->db->select("CONCAT(users.first_name, ' ' , users.last_name) AS full_name", FALSE); 
		}
		$this->db->from('groups');
		$this->db->join('group_members', 'group_members.group_id = groups.group_id');
		$this->db->join('users', 'users.user_id = group_members.user_id');
		$this->db->join('classes', 'users.class_id = classes.class_id', 'left');

		$this->db->where('groups.group_id', $group_id); 
		
		$query = $this->db->get();
		$result = $query->result_array();
		return $result;
	}

	public function get_students_group($user_id) {
		$this->db->select('group_id');
		$this->db->from('group_members');
		$this->db->where('user_id', $user_id); 
		$query = $this->db->get();
		if ($query->num_rows() > 0) {
			$result = $query->result_array();
			return $result;
		} else {
			return false;
		}
	}
	
	public function leave_group($user_id, $group_id) {
		$this->db->where('user_id', $user_id);
		$this->db->where('group_id', $group_id);
		$this->db->delete('group_members');
	}
	
	public function group_exists($group_name) {
		$this->db->where('group_name', $group_name);
		$this->db->from('groups');
		$query = $this->db->get();
		
		if ($query->num_rows() > 0) {
			return true;
		} else {
			return false;
		}
	}
	
	public function create_group($user_id, $group_name) {
		$data = array(
			'group_name'=>$group_name,
			'owner_id'=>$user_id
		);
		
		$this->db->insert('groups', $data);
		
		$this->db->where('group_name', $group_name);
		$this->db->from('groups');
		$query = $this->db->get();
		
		$result = $query->row_array();
		$this->add_student_to_group($user_id, $result['group_id']);
	}
	
	public function delete_group($group_id) {
		$this->db->where('group_id', $group_id);
		$this->db->delete('groups');
		
		$this->remove_students_from_group($group_id);
	}
	
	public function remove_students_from_group($group_id) {
		$this->db->where('group_id', $group_id);
		$this->db->delete('group_members');
	}
}